import {store} from '@/store/Store';

export interface Action {
  namespace?: string;
}

export function commit<TPayload>(ActionClass: { new(): TPayload; }, payload: TPayload) {
  const obj = new ActionClass();
  const namespace = (obj as unknown as Action).namespace;
  const type = namespace === undefined ? ActionClass.name : namespace + '/' + ActionClass.name;
  store.commit(type, payload);
}

export function dispatch<TPayload>(ActionClass: { new(): TPayload; }, payload: TPayload) {
  const obj = new ActionClass();
  const namespace = (obj as unknown as Action).namespace;
  const type = namespace === undefined ? ActionClass.name : namespace + '/' + ActionClass.name;
  return store.dispatch(type, payload);
}
